package com.test2.demo.controller;

import com.test2.demo.dao.courseDao;
import com.test2.demo.pojo.Course;
import com.test2.demo.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author
 * @create 86153-2021
 */

@Controller
public class HelloController {

    /**
     * 返回一个字符串
     *
     * @return
     */
    @GetMapping({"/say","hi","duank"})//映射端口，http://localhost:8081/hi
    @ResponseBody
    public String toHellow(){

        return "toHellow" ;
    }

    /**
     * 只返回一个静态界面
     * @return
     */
    @RequestMapping({"first","dddd"})

    public String tofirst(){

        return "first";//返回页面路径
    }

    /**
     * 返回界面中的按钮，可以实现进入下一个界面（必须实在controller里面已经返回过的）
     * @return
     */
    @RequestMapping({"/second"})
    public String toSecond(){
        return "second";
    }

    /**
     * 返回一个静态死界面
     * @return
     */
    @RequestMapping({"/static"})
    public String staticPage(){
        return "static";
    }


    /**
     * 这个是提前声明这个参数
     */
    Student student = new Student(1000001,"Shekai","男",2019,"软件工程","计算机学院");

    /**
     * 返回一个界面，界面带有参数，可以在界面陈列出来
     * 可以传入参数，在页面里面解析参数
     * @return
     */
    @RequestMapping({"/disPlayInformation"})
    public String disPlayPage(Model model){

        model.addAttribute("student",student);
        return "disPlayInformation";
    }

    /**
     * 到达选课界面
     * @return
     */
    @RequestMapping({"/chooseCourse"})
    public String chooseCourse(Model model){
        model.addAttribute("courses",courses.list());
        return "chooseCourse";
    }

    /**
     * 到达已选课程界面
     * @return
     */
    @RequestMapping({"/cheekCourse"})
    public String cheekCourse(Model model){
        model.addAttribute("courses",selects.list());
        return "cheekCourse";
    }


    /**
     * 到达主界面
     * @return
     */
    courseDao courses = new courseDao();//可选的课
    courseDao selects = new courseDao();//已选的课
    @RequestMapping({"/index"})
    public String index(){
        Course c0=new Course(0,"s","f1");
        Course c1=new Course(1,"r","f2");
        Course c2=new Course(2,"t","f3");
        Course c3=new Course(3,"y","f4");

        courses.initial(4);
        selects.initial(4);

        courses.add(c0);
        courses.add(c1);
        courses.add(c2);
        selects.add(c3);


        return "index";
    }

    /**
     * 选课提交的数据，返回数据到此个端口
     * @param avilibleCourse
     * @return
     */

    @RequestMapping("/selectCourse")
    public String selectCourse(int[] avilibleCourse){

        for (int i :avilibleCourse){
            selects.add(courses.select(i));
            courses.delete(courses.select(i));
        }
        return "redirect:/cheekCourse";
    }

    /**
     * 到达退课界面
     * @return
     */
    @RequestMapping({"/dropOut"})
    public String dropOut( int[] selectedCuorse){
        for (int i:selectedCuorse){
            selects.delete(courses.select(i));
            courses.add(courses.select(i));
        }
        return "redirect:/chooseCourse";
    }







}
